Blog

Why is Code Reuse Important?

Gavin Pickin May 02, 2022

Spread the word

Gavin Pickin

May 02, 2022

Spread the word


Share your thoughts

I know we all reuse code and some types of reuse are better than the others (I'm looking at you COPY AND PASTE / man in the mirror), but copy-paste is only a symptom of a larger issue, that WET code is not as easy to maintain as DRY code.

What is DRY Code?

Don't Repeat Yourself (DRY) code aims to reduce code repetition. We do not want to reinvent the wheel, but worse is having bad versions of the wheel scattered everywhere. The goal of DRY code is to refactor so that you only have one reusable instance of that code, and if you need to change it, you can do so in one place. It is easier to maintain and update. DRY code also reduces the chance of error since you are only updating one place instead of several. We have all used DRY code when we have used loops, functions, or includes.

What is WET code?

Write Everything Twice (WET) code is the opposite of DRY code. It is where you have duplicate code copy/pasted, or rewritten several times in multiple places. Sometimes WET code is DRY code that needs minor changes, but you make a copy of it. If it is a simple change, it might be ok to add it to your DRY code, but sometimes it's better not to over-engineer things. DRY code is the goal in most cases, and the benefits will outweigh the costs.

Why do we want DRY Code?

The biggest benefit of using DRY code is maintainability. Fixing bugs in one location is easier than remembering and finding all the occurrences of that logic. Likewise, updating the business logic or adding functionality is easier when the code is shared.

An Acronym used with DRY code is FIRST, which stands for Focused, Independent, Reusable, Small, and Testable (https://addyosmani.com/first/)

Readability

Programs must be written for people to read, and only incidentally for machines to execute - Harold Abelson

If a developer understands the principles of DRY code, they are more likely to understand clean code principles, leading to more readable code. Code reuse usually encourages smaller, more manageable functions, promoting lower cognitive load and making the code more readable.

Testing

DRY code is broken down into smaller pieces and is usually easier to test. The more code you have and the more paths and functions to cover, the harder it will be to test. Using DRY Code from libraries and frameworks means more eyes on it, and sometimes those libraries and frameworks have tests for their code already.

Cost

  • More code takes more time and money
  • Reinventing the wheel takes more time and money
  • Maintaining more takes costs more time and money
  • Testing more code takes more time and money
  • Fixing more bugs takes more time and money

How do we prevent Copy Pasting and WET Code?

We need fast and simple ways to share code in a project and between projects. We are lucky in ColdFusion in that we have lots of techniques and tools to help us write DRYer code. Check back for our next blog entry, where we go into more detail.

Add Your Comment

(1)

May 09, 2022 15:51:00 UTC

by Jason

Thanks for this Gavin. Tryng to convince other members in our crew to adopt this philosophy is somewhat challenging.

Recent Entries

Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

As we approach a stable release of BoxLang and our continued marketing reaches more folks, many have asked about its purpose. Why create a new language when the JVM ecosystem already includes established languages like Kotlin, Groovy, and Scala, to name a few.

Luis Majano
Luis Majano
December 18, 2024
ColdBox Free Tip 6 - Using Routing with Wildcard Domains!

ColdBox Free Tip 6 - Using Routing with Wildcard Domains!

ColdBox gives you the flexibility to create domain-specific routes, making it perfect for multi-tenant applications or projects that need to respond differently based on the domain or subdomain being accessed. In this tip, we’ll dive into how to use the withDomain() method to create routes that match specific domains or sub-domains.

Maria Jose Herrera
Maria Jose Herrera
December 18, 2024